<?php
/**
 * Public.php
 *
 * Momoger.com 根控制器类.
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://momoger.com/license
 *
 * @category	Momoger
 * @package 	Momoger_Controller
 * @author 		Guya
 * @version    	1.0
 * @copyright  	Copyright © 2009 www.momoger.com All Rights Reserved.
 * @license    	http://momoger.com/license     New BSD License
 */

/** Zend_Controller_Action */
//require_once 'Zend/Controller/Action.php';

/**
 * Momoger.com 根控制器类,初始化应用通用程序执行环境。
 *
 * @category	Momoger
 * @package 	Momoger_Controller
 * @copyright  	Copyright © 2009 www.momoger.com All Rights Reserved.
 * @license    	http://momoger.com/license     New BSD License
 */
class Momoger_Controller_Public extends Zend_Controller_Action {

	/**
	 * 系统logger记录器
	 *
	 * @var App_System_Logger
	 */
	protected $_logger = null;

	/**
	 * 系统缓存对象
	 *
	 * @var App_System_Cache
	 */
	protected $_cache = null;

	/**
	 * 当前用户信息
	 *
	 * @var stdClass
	 */
	protected $user = null;

    /**
     * 控制器初始化
     *
     * @return void
     */
    public function init() {

        //current user information
        $this->user = Zend_Auth::getInstance()->getIdentity();

        $this->view->nav = 'home';

        $this->view->style = 'default';

        //init anjuke controller init
        $this->mmInit();
    }

    /**
     * 获取系统日志记录对象
     *
     * @return App_System_Logger
     */
    public function logger() {
    	if (null === $this->_logger) {
			$this->_logger = App_System_Logger::getInstance();
    	}
    	return $this->_logger;
    }

    /**
     * 获取系统缓存对象
     *
     * @return App_System_Cache
     */
	public function cache() {
    	if (null === $this->_cache) {
			$this->_cache = App_System_Cache::getInstance();
    	}
    	return $this->_cache;
    }

    /**
     * 子类初始化接口.
     *
     * @return void
     */
    public function mmInit() {
    	//TODO init controller here.
    }

	/**
	 * 通用信息显示跳转页面
	 *
	 * @return HTML
	 */
	public function redirectorAction() {
		//todo
	}

	/**
	 * 系统页面重定向通用接口. 该接口不中断程序, 注意流程后添加 return 语句.
	 *
	 * @param int $delay 页面重定向等待时间.
	 * @param string $msg 页面重定向时显示的信息内容.
	 * @param array $urlParams 页面重定向到新的页面需要传递的参数. 包括URL重组参数.
	 * @param string $url 页面重定向到新URL地址.
	 * @return void
	 */
	public function redirectWithMsg($delay = 0, $msg = 'Page Redirected...', $urlParams = array(), $url = NULL) {
		//clear url required arguments
		if (!$url) {
			$urlParams = (array)$urlParams;
			if (!array_key_exists('module', $urlParams)) {
				$urlParams['module'] = 'home';
			}

			if (!array_key_exists('controller', $urlParams)) {
				$urlParams['controller'] = 'index';
			}

			if (!array_key_exists('action', $urlParams)) {
				$urlParams['action'] = 'index';
			}

			$redirectUrl = $this->getHelper('Url')->url($urlParams, null, TRUE);
		} else {
			$redirectUrl = $url;
		}

		//set delay time
		$this->getResponse()->setHeader('Refresh', $delay.'; url='.$redirectUrl);

		//set view data
		$this->view->redirect = array('msg' => $msg, 'delay' => $delay, 'url' => $redirectUrl);
		$this->_forward('redirector', 'index', 'home');
		return TRUE;
	}
}